package com.fzdy.dataplatform.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.fzdy.dataplatform.controller.api.DelArr;
import com.fzdy.dataplatform.controller.api.ModuleCallback;
import com.fzdy.dataplatform.controller.api.communication.CommunicationApi;
import com.fzdy.dataplatform.controller.api.sysLog.LogStringUtil;
import com.fzdy.dataplatform.controller.api.sysLog.SysLogApi;
import com.fzdy.dataplatform.core.common.AjaxCallback;
import com.fzdy.dataplatform.core.common.Check;
import com.fzdy.dataplatform.core.controller.BaseController;
import com.fzdy.dataplatform.core.vo.Page;
import com.fzdy.dataplatform.entity.CommunicationProtocol;
import com.fzdy.dataplatform.service.CommunicationProtocolService;
import com.fzdy.dataplatform.service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping(value = "/sysmanage/communicationprotocol")
public class CommunicationProtocolController extends BaseController {

    @Autowired
    private CommunicationProtocolService communicationProtocolService;
    @Autowired
    private ProductService productService;

    @RequestMapping(value = "/list")
    public String list(@RequestBody Page<CommunicationProtocol> page) {
        CommunicationProtocol communicationProtocol = new CommunicationProtocol(page.getName());
        communicationProtocolService.getPageByExampleEq(communicationProtocol, page);
        return JSON.toJSONString(new AjaxCallback(Communicationdbtoapi(page.getList()), page.getTotalRow()));
    }

    @RequestMapping(value = "/getprotocol")
    public String getprotocol(@RequestBody CommunicationApi api) {
        if (Check.checkObjNull(api.getId())) {
            AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "参数错误");
            return JSON.toJSONString(ajaxCallback);
        }
        CommunicationProtocol communicationProtocol = communicationProtocolService.get(api.getId());
        api = new CommunicationApi(communicationProtocol.getId(), communicationProtocol.getProtocol(), communicationProtocol.getMemo(), communicationProtocol.getPort());
        AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "请求成功");
        ajaxCallback.setData(api);
        return JSON.toJSONString(ajaxCallback);
    }


    @RequestMapping(value = "/edit")
    public String edit(@RequestBody CommunicationApi api, HttpServletRequest request) {
        AjaxCallback ajaxCallback;
        /** 日志*/
        SysLogApi log_sysLogApi = new SysLogApi(LogStringUtil.COMMUNICATIONPROTOCOL, LogStringUtil.SUCCESS,
                webUtilify.LoginUserinfo(request).getId());
        JSONObject log_content = new JSONObject();
        if (api.getId() != null) {
            log_sysLogApi.setOperation(LogStringUtil.UPDATA);
            CommunicationProtocol communicationProtocol = communicationProtocolService.get(api.getId());
            log_content.put(LogStringUtil.BEFOREEDIT, JSON.toJSONString(communicationProtocol));
            communicationProtocol.GeneralParams(api.getName(), api.getPort(), api.getMemo());
            log_content.put(LogStringUtil.AFTEREDIT, JSON.toJSONString(communicationProtocol));
            communicationProtocolService.updateT(communicationProtocol);
            ajaxCallback= new AjaxCallback(ModuleCallback.SUCCESSNO, "修改成功");
        } else {
            log_sysLogApi.setOperation(LogStringUtil.ADD);
            CommunicationProtocol communicationProtocol = new CommunicationProtocol(api.getName(), api.getPort(), api.getMemo());
            log_content.put("通信协议内容", JSON.toJSONString(communicationProtocol));
            communicationProtocolService.insertT(communicationProtocol);
            ajaxCallback= new AjaxCallback(ModuleCallback.SUCCESSNO, "添加成功");

        }
        log_sysLogApi.setContent(log_content);
        syslogService.WriteLog(log_sysLogApi);
        return JSON.toJSONString(ajaxCallback);
    }

    private List Communicationdbtoapi(List<CommunicationProtocol> list) {
        List<CommunicationApi> apis = new ArrayList<>();
        for (CommunicationProtocol communicationProtocol : list) {
            CommunicationApi api = new CommunicationApi(communicationProtocol.getId(), communicationProtocol.getProtocol(), communicationProtocol.getMemo(), communicationProtocol.getPort());
            apis.add(api);
        }
        return apis;
    }

    @RequestMapping(value = "/del")
    public String del(@RequestBody DelArr delArr, HttpServletRequest request) {

        communicationProtocolService.batchDelete(delArr.getIds());
        JSONObject log_content = new JSONObject();
        log_content.put("id", JSON.toJSONString(delArr.getIds()));
        syslogService.WriteLog(new SysLogApi(LogStringUtil.DEL, LogStringUtil.COMMUNICATIONPROTOCOL,
                LogStringUtil.SUCCESS, log_content, webUtilify.LoginUserinfo(request).getId()));
        AjaxCallback ajaxCallback = new AjaxCallback(ModuleCallback.SUCCESSNO, "删除成功");
        return JSON.toJSONString(ajaxCallback);
    }

}
